-- Exsto
-- Revanne's Quickmenu design

local PLUGIN = exsto.CreatePlugin()
	PLUGIN:SetInfo( {
		Name = "Quick Menu V2",
		ID = "quick-menu2",
		Desc = "A plugin that creates a quick menu page.",
		Owner = "Prefanatic",
	} )


if SERVER then

elseif CLIENT then

	-- little function open our list :)
	concommand.Add( "+ExQuick", function()
		if !exsto.Ranks then print( "Ranks non-existant on client.  Please wait..." ) return end
		if !PLUGIN.PlayerList then -- wtf  shouldn't happen.
			PLUGIN:Init()
		end
		
		PLUGIN.PlayerList:Show()
	end )
	
	concommand.Add( "-ExQuick", function()
		PLUGIN.PlayerList:Hide()
	end )

	function PLUGIN:Init()
		
		-- We need to create our derma for the user list, to start with.
		-- No DFrame, just DPanels love.
		self.PlayerList = self:CreatePlayerPanel()
		
	end
	
	local function playerObjectClick( obj )
		-- TODO
	end
	
	local function playerObjectPaintOver( obj )
		-- TODO
	end
	
	function PLUGIN:CreatePlayerPanel()
		
		-- DO we need X + Y?  I think we might just move it around without it.
		local panel = exsto.CreatePanel( 0, 0, 266, 450, nil )
			panel:SetSkin( "ExstoTheme" )
			panel.bgColor = Color( 237, 237, 237, 255 )
			panel.PaintOver = function( pnl )
				surface.SetDrawColor( 125, 125, 125, 255 )
				surface.DrawOutlinedRect( 0, 0, pnl:GetWide(), pnl:GetTall() )
				
				surface.SetTexture( exsto.Textures.QuickLogo )
				surface.DrawTexturedRect( pnl:GetWide() - 128, 0, 128, 32 )
			end
			panel:SetVisible( false )
			
			panel.backButton = exsto.CreateButton( 7, 7, 30, 20, "", panel )
				panel.backButton.disabled = true
				panel.backButton.Paint = function( pnl )
					surface.SetDrawColor( 255, 255, 255, 255 )
					surface.SetTexture( !pnl.disabled and exsto.Textures.BackHighlight or exsto.Textures.BackNorm )
					surface.DrawTexturedRect( 0, 0, 32, 32 )
				end
				panel.backButton.DoClick = function( pnl )
					if pnl.disabled then return end
					
					panel.content:SlideBack()
					pnl.disabled = true
				end
				
			local function holderPaint( pnl )
				surface.SetDrawColor( 246, 246, 246, 255 )
				surface.DrawRect( 0, 0, pnl:GetWide(), pnl:GetTall() )
				
				surface.SetDrawColor( 222, 222, 222, 255 )
				surface.DrawOutlinedRect( 0, 0, pnl:GetWide(), pnl:GetTall() )		
			end
				
			-- Here is the profile page.  Just incase yous were wondering.
			panel.Profile = exsto.CreatePanel( 5, 32, panel:GetWide() - 10, panel:GetTall() - 37, nil, panel )
				panel.Profile.Paint = holderPaint
				panel.Profile.PaintOver = function( pnl )
					surface.SetDrawColor( 223, 223, 223, 255 )
					surface.DrawLine( 6, 60, pnl:GetWide() - 12, 60 )
				end
				panel.Profile.Update = function( pnl )
					pnl.Avatar:SetPlayer( pnl.Player )
					pnl.Name:SetText( pnl.Player:Nick() )
					pnl.Name:SizeToContents()
					
					pnl.Rank:SetText( pnl.Player:GetNiceRank() )
					pnl.Rank:SizeToContents()
					
					pnl:InvalidateLayout()
				end 
				
			panel.Profile.AvatarBG = exsto.CreatePanel( 6, 12, 40, 40, nil, panel.Profile )
				panel.Profile.AvatarBG.col = Color( 197, 197, 197, 255 )
				panel.Profile.AvatarBG.Paint = function( pnl )
					draw.RoundedBox( 4, 0, 0, pnl:GetWide(), pnl:GetTall(), pnl.col )
				end
				
				panel.Profile.Avatar = vgui.Create( "AvatarImage", panel.Profile.AvatarBG )
					panel.Profile.Avatar:SetPos( 4, 4 )
					panel.Profile.Avatar:SetSize( 32, 32 )
					
				panel.Profile.Name = exsto.CreateLabel( 53, 12, "UNKNOWN", "ExGenericText18", panel.Profile )
				panel.Profile.Rank = exsto.CreateLabel( 53, 32, "UNKNOWN", "ExRankText", panel.Profile )
				
			panel.content = exsto.CreatePanelList( 5, 32, panel:GetWide() - 10, panel:GetTall() - 37, 0, false, true, panel )
				panel.content.Paint = holderPaint
				panel.content.SlideAway = function( pnl )
					//pnl:SetPos( 5, pnl:GetTall() + 40 )
					pnl:SetVisible( false )
				end
				panel.content.SlideBack = function( pnl )
					//pnl:SetPos( 5, 32 )
					pnl:SetVisible( true )
				end
				
				Menu:CreateAnimation( panel.content )
					panel.content:FadeOnVisible( true )
				
			local function alphaSort( ply1, ply2 )
				return ply1:Nick() < ply2:Nick()
			end
			
			local function emptyFunc() end
				
			panel.CreateLayout = function( pnl )
				pnl.Layout = {}
				
				for _, ply in pairs( player.GetAll() ) do
					pnl.Layout[ ply:GetRank() ] = pnl.Layout[ ply:GetRank() ] or {}
					
					if !table.HasValue( pnl.Layout[ ply:GetRank() ], ply ) then
						table.insert( pnl.Layout[ ply:GetRank() ], ply )
					end
				end
				
				-- Sort the players in their respective areas alphabetically
				for rank, plyTbl in pairs( pnl.Layout ) do
					table.sort( plyTbl, alphaSort )
				end
			end
			
			local function boxPaint( box )
			
			end
			
			local function objClick( obj )
				if panel.selectedSlot then
					if panel.selectedSlot == obj then -- Holy shit!  Time to migrate into the profile page :)
						panel.selectedSlot:SetSelected( false )
						
						-- Player updates.
						panel.Profile.Player = obj.plyObj
						panel.Profile:Update()
						
						panel.selectedSlot = nil
						panel.backButton.disabled = false
						panel.content:SlideAway()
						return
					end
					panel.selectedSlot:SetSelected( false )
				end
				
				panel.selectedSlot = obj
				//obj:GetParent():SelectItem( obj, true )
			end
			
			panel.CreateBox = function( pnl, plyTbl )
				local box = exsto.CreateComboBox( 0, 0, 0, 0 )
					box:SetAutoSize( true )
					box.Paint = boxPaint
					
					local obj
					for _, ply in pairs( plyTbl ) do
						obj = box:AddItem( ply:Nick() )
							obj.plyObj = ply
							obj.DoClick = objClick
							obj.OnCursorMoved = emptyFunc
					end
					
				return box
			end

			panel.CreateControls = function( pnl )
				pnl.Controls = pnl.Controls or {}
				local category, plyTbl
				pnl.content:Clear()
				
				-- Loop through our layout.
				for short, data in SortedPairsByMemberValue( exsto.Ranks, "Immunity", false ) do
					if pnl.Layout[ short ] then
						category = data.Name
						plyTbl = pnl.Layout[ short ]
						
						if !pnl.Controls[ category ] then -- If the control doesn't exist yet...
							pnl.Controls[ category ] = exsto.CreateCollapseCategory( 3, 3, panel.content:GetWide() - 6, 10, category, panel.content )
							
							local box = pnl:CreateBox( plyTbl )
							pnl.Controls[ category ].Box = box
							
							pnl.Controls[ category ]:SetContents( pnl.Controls[ category ].Box )
							pnl.content:AddItem( pnl.Controls[ category ] )
							pnl.Controls[ category ]:SetExpanded( true )
						else -- If he does exist, we need to update him.
							pnl.Controls[ category ].Box:Remove()
							
							local box = pnl:CreateBox( plyTbl )
							pnl.Controls[ category ].Box = box
							
							pnl.Controls[ category ]:SetContents( pnl.Controls[ category ].Box )
							pnl.content:AddItem( pnl.Controls[ category ] )
							
							pnl.Controls[ category ]:Toggle()
							timer.Simple( 0.01, pnl.Controls[ category ].Toggle, pnl.Controls[ category ] ) -- Score one for VGUI hacks.
						end
					end
				end
			end
			
			panel.Update = function( pnl )
				panel.selectedSlot = nil
				
				pnl:CreateLayout()
				pnl:CreateControls()
			end
			
		Menu:CreateAnimation( panel )
		panel:FadeOnVisible( true )
		
		-- Show/Hide handles
		panel.Show = function( pnl )
			gui.EnableScreenClicker( true )
			pnl:SetVisible( true )
			pnl:SetPos( ( ScrW() / 2 ) - ( pnl:GetWide() / 2 ), ( ScrH() / 2 ) - ( pnl:GetTall() / 2 ) )
			
			-- Update while we are at it.
			pnl:Update()
		end
		
		panel.Hide = function( pnl )
			gui.EnableScreenClicker( false )
			pnl:SetPos( -( pnl:GetWide() + 10 ), ( ScrH() / 2 ) - ( pnl:GetTall() / 2 ) )
			pnl:SetVisible( false )
		end
		
		return panel
	end		

end	

PLUGIN:Register()